#include "me.h"
#include "ui_me.h"
#include<QDebug>

Me::Me(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Me)
{
    ui->setupUi(this);


    this->getAllUsers(); //读取文件中的用户信息

    this->loginUser = nullptr; //当前登录的用户指针置为空
    this->isLogin = false;

    this->login = new Login(users); //新建登录界面
//    ui->userName->setText("未登录");
    //ui->userName->setStyleSheet("font: 18pt Agency FB");

    this->regist = new Regist(users); //新建注册界面
    this->paihang = new PaiHang(this->users); //新建排行榜界面
//    this->toChatRoom = new ToChatRoom; //新建toChatRoom界面
    this->history = new History; //新建历史记录界面
    this->changePwd = new ChangePwd;
    this->changeNickName = new ChangeNickName;
    this->sureDelAccount = new SureDelAccount;
    this->initScene(); //初始化窗口

}

Me::~Me()
{
    delete ui;
}

void Me::on_login_clicked()
{
    this->login->users = this->users; //保持登录窗口中的用户信息与我窗口中的一致
    emit this->showLogin(); //发送信号显示登录界面
    this->hide(); //隐藏当前窗口
}

//显示当前界面
void Me::showMe(){
    //QSound::play(GAME_BGM_GAME_MENU);
//    if(this->login){
//        this->saveUserToFile();
//    }
//    this->receiver->bind(56666/*this->loginUser->post.toInt()*/, QUdpSocket::ShareAddress); //test代码

    this->show(); //显示当前界面


}

void Me::on_back_clicked()
{
    emit this->showWidget(); //发送信号显示主窗口
    this->hide(); //隐藏当前窗口
}

//接收当前登录用户
void Me::recLoginUser(User *user){  
    this->isLogin = true;
//    this->getAllUsers(); //读取文件
    this->loginUser=user;
    ui->userName->setText(this->loginUser->userName); //修改当前登录用户名字
    ui->score->setText(this->loginUser->score); //修改分数
    ui->nickName->setText(this->loginUser->nickName); //修改当前登录用户昵称

    emit this->sendChangeScoreInMainWidget(); //发送信号修改主窗口的分数

    this->show(); //显示当前界面
}

void Me::saveUserToFile(){
    QFile file("./user.txt");
    file.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text);
    QTextStream out(&file);
    out.setCodec("UTF-8");
    for(int i=0;i<this->users.size();i++){
       if(i==0){
            out<<this->users.at(i)->userName<<" "<<this->users.at(i)->passWord
              <<" "<<this->users.at(i)->score << " " << this->users.at(i)->post << " " << this->users.at(i)->nickName;
            continue;
       }
            out<<"\n"<<this->users.at(i)->userName<<" "<<this->users.at(i)->passWord
              <<" "<<this->users.at(i)->score << " " <<  this->users.at(i)->post << " " << this->users.at(i)->nickName;
        }
        file.close();
}

void Me::on_regist_clicked()
{
    this->regist->users = this->users; //保持注册窗口中的用户信息与我窗口中的一致
    //读取文件中的端口
    this->regist->postVec.clear(); //清空
    for(int i = 0; i < this->users.size(); i++){
        this->regist->postVec.push_back(this->users.at(i)->post);
    }

    emit this->showRegist();
    //QSound::play(GAME_BGM_GAME_MENU);
    this->hide(); //隐藏当前窗口
}

void Me::recRegistUser(User *user){
    isLogin = true;
    this->loginUser = user; //改变当前登录用户
    ui->userName->setText(this->loginUser->userName); //修改当前登录用户名字
    //ui->userName->setText("font: 18pt Agency FB");
//    this->login->users.append(new User(user->userName, user->passWord, user->score, user->post)); //将新注册用户添加到链表User中
    this->users.append(new User(user->userName, user->passWord, user->score, user->post, user->nickName)); //将新注册用户添加到链表User中

    this->saveUserToFile(); //保存所有用户的信息
    ui->score->setText(this->loginUser->score); //修改分数
    ui->nickName->setText(this->loginUser->nickName); //修改当前登录用户昵称

    emit this->sendChangeScoreInMainWidget(); //发送信号修改主窗口的分数

    this->show(); //显示当前界面
//    //修改排行榜中的信息
//    this->paihang->temp = this->users;
//    this->paihang->userNum = this->paihang->temp.size();
    //this->paihang->zhiZuo();//
}

//初始化窗口
void Me::initScene(){
    setWindowTitle(GAME_NAME); //设置窗口标题
//    setWindowIcon(QIcon(GAME_ICON_)); //设置窗口图标
//    setFixedSize(600, 800); //设置窗口大小
}

void Me::on_paiHang_clicked()
{
    if(this->isLogin){
        //修改排行榜中的信息
        this->paihang->temp = this->users;
        this->paihang->userNum = this->paihang->temp.size();

        emit this->sendZhiZuoPaiHang(); //发送信号制作排行榜
        this->paihang->show(); //显示排行榜窗口


         //QSound::play(GAME_BGM_GAME_MENU);

         this->hide(); //隐藏当前窗口
    }else{
        QMessageBox::warning(NULL, GAME_NAME, "请登录！");
    }


}

void Me::on_save_clicked()
{
    this->modUser(); //修改用户分数

    if(this->isLogin){
        this->saveUserToFile(); //保存用户信息
        emit sendNameToHistory(this->loginUser->userName, this->loginUser->nickName); //发送信号把用户昵称给历史记录界面
        emit sendUpdateHistory(this->loginUser->score); //发送信号更新历史记录
        QMessageBox::information(NULL, GAME_NAME, "保存成功！");
    }else{
        QMessageBox::warning(NULL, GAME_NAME, "保存失败，请登录！");
    }

}

//从文件获取所有的用户
void Me::getAllUsers(){
    this->users.clear(); //清空链表
    //读取文件
    QFile file ("./user.txt");
    file.open(QIODevice::ReadOnly| QIODevice::Text );
    QByteArray t=file.readAll();
    QString alltext=QString(t);
    QStringList list=alltext.split("\n");
    for (int i=0; i<list.size() ;i++ ) {
        QStringList temp=list[i].split(" ");
        if(temp.size()>4){
            users.append(new User(temp[0],temp[1], temp[2], temp[3], temp[4]));
        }
    }
    file.close();
    //() << users.at(1)->score;
    //往user中临时存数据
    //users.append(new User("123123","123123"));
}

//修改用户信息
void Me::modUser(){
    for(int i = 0; i < this->users.size(); i++){
        if(this->isLogin && this->users.at(i)->userName.compare(this->loginUser->userName) == 0){
            this->users.at(i)->score = this->loginUser->score; //改变分数
            ui->score->setText(this->loginUser->score); //改变ui界面的分数
        }
    }
}


void Me::recScore(QString score){
    this->loginUser->score = score;
}

//在我界面显示“您有新消息”
void Me::recNewMessageToMe()
{
    ui->newMessage->setText("您有新消息");
}

//在我界面清除“您有新消息”
void Me::recClearNewMessage()
{
    ui->newMessage->setText("暂无新消息");
}

void Me::recNewPwdToMe(QString newPwd)
{
    this->loginUser->passWord = newPwd;
    for(int i = 0; i < this->users.size(); i++){
        if(this->loginUser->userName.compare(this->users.at(i)->userName) == 0){ //如果当前登录用户的用户名与链表中的相等
            this->users.at(i)->passWord = newPwd;
        }
    }

    this->saveUserToFile(); //更新文件中的数据
}

void Me::recNewNickNameToMe()
{
    this->loginUser->nickName = this->changeNickName->newNickName;
    for(int i = 0; i < this->users.size(); i++){
        if(this->loginUser->userName.compare(this->users.at(i)->userName) == 0){ //如果当前登录用户的用户名与链表中的相等
            this->users.at(i)->nickName = this->changeNickName->newNickName;
        }
    }

    ui->nickName->setText(this->changeNickName->newNickName); //修改ui界面中的用户昵称

    this->saveUserToFile(); //更新文件中的数据
}

//接收信号确定注销账号
void Me::recSureDelAccountToMe()
{
    this->isLogin = false; //改变登录状态为未登录

    for(int i = 0; i < this->users.size(); i++){
        if(this->loginUser->userName.compare(this->users.at(i)->userName) == 0){ //如果当前登录用户的用户名与链表中的相等
            this->users.erase(this->users.begin() + i);
        }
    }

    this->saveUserToFile(); //更新文件中的数据

    //清空该用户相关文件中的内容
    QString fileName;
    fileName = "./";
    fileName += this->loginUser->userName;
    fileName += "History.txt";
    QFile file(fileName);
    file.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text);
    QTextStream out(&file);
    out.setCodec("UTF-8");
    file.close();

    fileName = "./";
    fileName += this->loginUser->userName;
    fileName += "MyFriend.txt";
    file.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text);
    out.setCodec("UTF-8");
    file.close();

    //修改ui界面中有关登录用户的信息
    ui->userName->setText("未登录");
    ui->score->setText("请登录");
    ui->nickName->setText("登录后查看");
    ui->newMessage->setText("暂无新消息");

    //刷新当前界面
    this->hide();
    this->show();
}

//接收信号改变我的界面的分数
void Me::recChangeScoreInMeUi()
{
    ui->score->setText(this->loginUser->score);
}



//void Me::on_toChatRoom_clicked()
//{
//    if(this->isLogin){
//        this->toChatRoom->charRoom->loginUser = new User(this->loginUser->userName, this->loginUser->passWord,
//                                                             this->loginUser->score, this->loginUser->post, this->loginUser->nickName); //修改聊天室内的当前用户
//        emit this->showToChatRoom();
//        this->hide();
//    }else{
//        QMessageBox::warning(NULL, GAME_NAME, "登录后才能建立新连接");
//    }


//}

//void Me::on_chatRoom_clicked()
//{
//    if(this->toChatRoom->isNewConnectContrust){
//        this->toChatRoom->charRoom->loginUser = new User(this->loginUser->userName, this->loginUser->passWord,
//                                                             this->loginUser->score, this->loginUser->post, this->loginUser->nickName); //修改聊天室内的当前用户
//        emit showChatRoom(); //显示聊天室
//        this->hide();
//    }else{
//        QMessageBox::warning(NULL, GAME_NAME, "请先建立新连接再进入聊天室！", QMessageBox::Ok);
//    }
//}

void Me::on_history_clicked()
{
    if(this->loginUser){
        emit sendNameToHistory(this->loginUser->userName, this->loginUser->nickName); //发送信号把用户名和昵称给历史记录界面
        emit sendShowHistory(); //发送信号显示历史记录
        if(this->history->hasHistory){
            this->hide();
        }
    }else{
        QMessageBox::warning(NULL, GAME_NAME, "登录后才能查看历史记录！");
    }

}

void Me::on_changePwd_clicked()
{
    if(this->isLogin){
        //传递信息给修改密码界面
        this->changePwd->userName = this->loginUser->userName;
        this->changePwd->oldPwd = this->loginUser->passWord;

        emit sendShowChangePwd(); //发送信号显示修改密码界面
        this->hide();
    }else{
        QMessageBox::warning(NULL, GAME_NAME, "登录后才能修改密码！");
    }
}

void Me::on_changeNickName_clicked()
{
    if(this->isLogin){
        emit sendShowChangeNickName(); //发送信号显示修改昵称界面
        this->hide();
    }else{
        QMessageBox::warning(NULL, GAME_NAME, "登录后才能修改昵称！");
    }
}

void Me::on_delAccount_clicked()
{
    if(this->isLogin){
        emit sendShowSureDelAccount(); //发送信号显示确认注销账号界面
    }else{
        QMessageBox::warning(NULL, GAME_NAME, "登录后才能注销账号！");
    }

}

void Me::on_exitAccount_clicked()
{
    if(this->isLogin){
        this->isLogin = false; //改变登录状态为未登录

        //修改ui界面中有关登录用户的信息
        ui->userName->setText("未登录");
        ui->score->setText("请登录");
        ui->nickName->setText("登录后查看");
        ui->newMessage->setText("暂无新消息");

        //刷新当前界面
        this->hide();
        this->show();
        QMessageBox::information(NULL, GAME_NAME, "退出成功！");
    }else{
        QMessageBox::warning(NULL, GAME_NAME, "登录后才能退出账号！");
    }
}
